package com.wlyuan.open.controller;

import com.wlyuan.core.exception.ValueError;
import com.wlyuan.open.api.IOpenClientService;
import com.wlyuan.web.claim.Claim;
import com.wlyuan.web.claim.ClaimUtils;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;

/**
 * @author lijing
 */
@Slf4j
public class BaseController {
    @DubboReference(check = false)
    private IOpenClientService openClientService;

    public String getOpenMobile() {
        var client = openClientService.getOpenClientByAppId(getAppId());
        if (client == null) {
            throw new ValueError("开放账号不存在");
        }
        return client.getMobile();
    }

    public String getAccountMobile(String account) {
        return openClientService.getUserMobile(account);
    }

    public String getAppId() {
        var appId = getClaim().getAppId();
        if (StringUtils.isBlank(appId)) {
            throw new ValueError("请先登录");
        }
        return appId;
    }

    private Claim getClaim() {
        var claim = ClaimUtils.get();
        logger.info("login claim : {}", claim);
        if (claim == null) {
            throw new ValueError("请先登录");
        }
        return claim;
    }
}
